﻿package com.badibadi.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;

import com.badibadi.mytools.Constants;
import com.badibadi.mytools.Convert;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.badibadi.uniclubber.R.layout;
import com.badibadi.uniclubber.R.menu;
import com.badibadi.adapter.FragAdapter;
import com.badibadi.fragment.ExpertAppleyForChoseTypeFragment;
import com.badibadi.fragment.ExpertAppleyForChoseTypeFragment.CallBack;
import com.badibadi.fragment.ExpertAppleyForChoseTypeFragment.OnDataChangeClistener;
import com.badibadi.infos.CreateNewClubChoseTypeInfos;
import com.badibadi.infos.ExpertApplyChoseTypePictureInfos;
import com.badibadi.infos.MasterModel;
import com.badibadi.infos.Results;
import com.badibadi.infos.UserInfoModel;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class ExpertApplyForChoseTypeActivity extends BaseActivity implements
		OnDataChangeClistener, OnClickListener {
	private LinearLayout importlayout;
	private ExpertAppleyForChoseTypeFragment eafcFragment;

	private List<Fragment> mViewFragmentHolders;
	/**
	 * 每页的数量
	 */
	private final int IMGNUMBER = 9;
	private int zongyeshu;
	private ViewPager vp;
	private FragAdapter adapter;
	private FragmentManager manager;
	// /**
	// * 保存要上传的，这里面的需要上传的才是主要的；
	// */
	// public Map<Integer, View> MapDate;
	// 保存需要传的数据
	private String Date = "";
	private RadioGroup mRadioGroup;
	/** 需要重写的第一个 ：总的数量 **/
	public int ItemNum = 0;
	/** 保存当前页数 */
	private int page;

	public List<MasterModel> masterListView;
	/**
	 * 下一步按钮
	 */
	private Button next_bu;
	/**
	 * 只是用于交换数据的
	 */
	public List<String> list = new ArrayList<String>();

	public android.os.Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 0:
				Utils.ExitPrgress(ExpertApplyForChoseTypeActivity.this);
				initItemNum(masterListView.size());
				initFragment();
				break;
			case 1:
				Utils.ExitPrgress(ExpertApplyForChoseTypeActivity.this);
//				   
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		goToTheTitle();
		// android4Detail();
		setContentView(R.layout.expert_apply_for_chose_type);
//		   
		masterListView = new ArrayList<MasterModel>();
		init();
		initInterNet();

	}

	/**
	 * 重写四处 复用的话重写它（第一处） 要重写它
	 */
	protected void initInterNet() {
		// TODO Auto-generated method stub
		Utils.showPrgress(ExpertApplyForChoseTypeActivity.this);
		new Thread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("languageType",
						com.badibadi.mytools.Dialog
								.getSystemLanguageType(ExpertApplyForChoseTypeActivity.this));
				String content = Utils.sendRequest(map, Constants.BadiUrl
						+ "master");
				//    
				if (content != null) {
					//    
					Results results = Utils.checkResult_NNN(
							ExpertApplyForChoseTypeActivity.this, content);
					if (results != null && results.getRetmsg() != null) {
//						   
						try {
							masterListView = JSONUtils.getListByJsonString(
									results.getRetmsg(), MasterModel.class);
							for (int i = 0; i < masterListView.size(); i++) {
								list.add(masterListView.get(i).getName());
							}
							ItemNum = masterListView.size();
							mHandler.sendEmptyMessage(0);

						} catch (Exception e) {
							// TODO Auto-generated catch block
//							   
							   
						}
					}
				} else {
					Utils.ExitPrgress(ExpertApplyForChoseTypeActivity.this);
					mHandler.sendEmptyMessage(1);
				}
			}
		}).start();

	}

	/** 复用的话重写它 */
	protected void initItemNum(int num) {
		// TODO Auto-generated method stub
		ItemNum = num;
	}

	private void init() {
		// TODO Auto-generated method stub
		mRadioGroup = (RadioGroup) this.findViewById(R.id.feac_redio_group);
		next_bu = (Button) this.findViewById(R.id.next_step);
		next_bu.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Date = ((ExpertAppleyForChoseTypeFragment) mViewFragmentHolders
						.get(page)).getChoseData();
//				   

			}
		});
		this.findViewById(R.id.expert_apply_for_chose_Return_btn)
				.setOnClickListener(this);
	}

	/** 设置Data后获得数据(这里获得需要传给下一个activity的数据) */
	public String GetData() {
		if (mViewFragmentHolders != null) {
			Date = ((ExpertAppleyForChoseTypeFragment) mViewFragmentHolders
					.get(page)).getChoseData();
		}
		return Date;
	}

	/**
	 * 获取Data的Id
	 * 
	 * @return
	 */
	public String GetDataId() {
		String str = GetData().trim();
//		   
		for (int i = 0; i < masterListView.size(); i++) {
//			   
			if (str.equals(masterListView.get(i).getName().trim())) {
				return masterListView.get(i).getId();
			} else {

			}
		}
		return "-1";
	}

	/**
	 * 将viewpage和fragment结合起来使用
	 */
	private void initFragment() {
		// MapDate=new HashMap<Integer, View>();

		mViewFragmentHolders = new ArrayList<Fragment>();
		vp = (ViewPager) findViewById(R.id.expert_apply_cType_viewpager);
		// TODO Auto-generated method stub
		importlayout = (LinearLayout) this
				.findViewById(R.id.expert_apply_cType_layout);
		// 获取总页数
		zongyeshu = ItemNum / IMGNUMBER;
		// 多出来的就加一个
		if ((ItemNum) % IMGNUMBER != 0) {
			zongyeshu++;
		}
		for (int i = 0; i < zongyeshu; i++) {
			//    
			ExpertAppleyForChoseTypeFragment eafct = new ExpertAppleyForChoseTypeFragment();
			eafct.setThisPage(i);
			eafct.setOnDataClistener(ExpertApplyForChoseTypeActivity.this);
			mViewFragmentHolders.add(eafct);
			// 添加radioButton

		}
		// 这里是添加radioButton
		addRadioButton();

		adapter = new FragAdapter(getSupportFragmentManager(),
				mViewFragmentHolders);
		vp.setAdapter(adapter);
		// 从第0也开始
		vp.setCurrentItem(0);
		// 预先加载的页数
		vp.setOffscreenPageLimit(0);
		vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			// 转到当前的页面
			@Override
			public void onPageSelected(int pos) {
				// TODO Auto-generated method stub
				page = pos;
				RefreshPage(page);
//				   
			}

			@Override
			public void onPageScrolled(int position, float arg1, int arg2) {
				// TODO Auto-generated method stub
				//    
				// "onPageScrolled postion_arg1_arg2-->"+position+"//"+arg1+"//"+arg2);
			}

			// 交换页数的时候
			@Override
			public void onPageScrollStateChanged(int position) {
				// TODO Auto-generated method stub

				// ExpertAppleyForChoseTypeFragment
				// eafc=(ExpertAppleyForChoseTypeFragment)(adapter.getItem(position-1));
				// eafc.initExpertAppleyForChoseTypeView();
				//    

			}
		});

		// .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
		// {
		//
		// @Override
		// public void onCheckedChanged(RadioGroup group, int checkedId) {
		// // TODO Auto-generated method stub
		// //    
		// if(group.getChildAt(0).getId()==R.id.feac_radio0)
		// {
		// ThisPage=0;
		// initExpertAppleyForChoseTypeView();
		// }
		// //子数量
		// int number=group.getChildCount();
		// for(int i=1;i<number;i++)
		// {
		// if(group.getCheckedRadioButtonId()==i)
		// {
		// ThisPage=i;
		// initExpertAppleyForChoseTypeView();
		// }
		// }
		// switch(group.getCheckedRadioButtonId())
		// {
		// case 1:
		// ThisPage=1;
		// initExpertAppleyForChoseTypeView(view);
		// // Toast.makeText(getActivity(), "再次成功", 1).show();
		// break;
		// case 2:
		// ThisPage=2;
		// initExpertAppleyForChoseTypeView(view);
		// break;
		// }
		//
		// }
		// });

		// importlayout.addView(vp);
	}

	/**
	 * 添加radioButton
	 */
	private void addRadioButton() {
		// TODO Auto-generated method stub
		for (int i = 0; i < zongyeshu - 1; i++) {
			RadioButton rb = new RadioButton(
					ExpertApplyForChoseTypeActivity.this);
			rb.setId(i);
			rb.setText("");
			rb.setClickable(false);
			mRadioGroup.addView(rb);
		}
	}

	/**
	 * 刷新上一页和下一页 （或者上一页或者下一页）
	 * 
	 * @param p
	 */
	protected void RefreshPage(int p) {
		// TODO Auto-generated method stub

		mRadioGroup.check(mRadioGroup.getChildAt(p).getId());
		if (p == 0) {
			ExpertAppleyForChoseTypeFragment Eafctf = ((ExpertAppleyForChoseTypeFragment) (adapter
					.getItem(p + 1)));

			Eafctf.getJiugongItem(new CallBack() {

				@Override
				public void getFragmentView(View view, int Page,
						int jiugonggeitem, ImageView[] img_gou,
						ImageView[] img_zhezhao, String ChoseData) {
					// TODO Auto-generated method stub
//					   
					if (jiugonggeitem != -1) {
						img_gou[jiugonggeitem].setVisibility(View.INVISIBLE);
						img_zhezhao[jiugonggeitem].setVisibility(View.VISIBLE);
						ChoseData = "";
					}

				}
			});
		} else if (p == zongyeshu - 1) {
			ExpertAppleyForChoseTypeFragment Eafctf = ((ExpertAppleyForChoseTypeFragment) (adapter
					.getItem(p - 1)));

			Eafctf.getJiugongItem(new CallBack() {

				@Override
				public void getFragmentView(View view, int Page,
						int jiugonggeitem, ImageView[] img_gou,
						ImageView[] img_zhezhao, String ChoseData) {
					// TODO Auto-generated method stub
//					   
					if (jiugonggeitem != -1) {
						img_gou[jiugonggeitem].setVisibility(View.INVISIBLE);
						img_zhezhao[jiugonggeitem].setVisibility(View.VISIBLE);
						ChoseData = "";
					}

				}
			});
		} else {
			ExpertAppleyForChoseTypeFragment Eafctf = ((ExpertAppleyForChoseTypeFragment) (adapter
					.getItem(p + 1)));

			Eafctf.getJiugongItem(new CallBack() {

				@Override
				public void getFragmentView(View view, int Page,
						int jiugonggeitem, ImageView[] img_gou,
						ImageView[] img_zhezhao, String ChoseData) {
					// TODO Auto-generated method stub
//					   
					if (jiugonggeitem != -1) {
						img_gou[jiugonggeitem].setVisibility(View.INVISIBLE);
						img_zhezhao[jiugonggeitem].setVisibility(View.VISIBLE);
						ChoseData = "";
					}

				}
			});
			ExpertAppleyForChoseTypeFragment Eafctf2 = ((ExpertAppleyForChoseTypeFragment) (adapter
					.getItem(p - 1)));

			Eafctf2.getJiugongItem(new CallBack() {

				@Override
				public void getFragmentView(View view, int Page,
						int jiugonggeitem, ImageView[] img_gou,
						ImageView[] img_zhezhao, String ChoseData) {
					// TODO Auto-generated method stub
//					   
					if (jiugonggeitem != -1) {
						img_gou[jiugonggeitem].setVisibility(View.INVISIBLE);
						img_zhezhao[jiugonggeitem].setVisibility(View.VISIBLE);
						ChoseData = "";
					}
				}
			});
		}
	}

	// @Override
	// public void initData(String[] tupianming, String[] beijingtu) {
	// // TODO Auto-generated method stub
	//    
	//
	// // TODO Auto-generated method stub
	// beijingtu = ;
	// tupianming=;
	// }
	@Override
	public String[] getTuPianMing(String[] tupianming) {
		// TODO Auto-generated method stub
		// 在这里，不满九张的最好加上,不知道为什么，不加的话图片会拉长
		if (list.size() % 9 != 0) {
			for (int i = 0; i < list.size() % 9; i++) {
				list.add("");
			}
		}
		return Convert.ListTransitionString(list);
	}

	@Override
	public String[] getBeiJingTu(String[] beijingtu) {
		// TODO Auto-generated method stub
		if (list.size() % 9 != 0) {
			for (int i = 0; i < list.size() % 9; i++) {
//				list.add("http://pica.nipic.com/2008-01-12/20081129186810_2.jpg");
				list.add("http://img2.imgtn.bdimg.com/it/u=3107998002,1911997068&fm=23&gp=0.jpg");
			}
		}
		ExpertApplyChoseTypePictureInfos experInfos = new ExpertApplyChoseTypePictureInfos(
				this);
		return experInfos.getPic();
	}

	public List<MasterModel> getMasterListView() {
		return masterListView;
	}

	public void setMasterListView(List<MasterModel> masterListView) {
		this.masterListView = masterListView;
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.expert_apply_for_chose_Return_btn:
			this.finish();
			break;

		default:
			break;
		}
	}

}
